
<%var csstpl = {%>
    <link href="${basepath}/vendor/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <style>
    	.btn { margin-bottom:5px; }
    	.btn_reload { color:#fff !important; }
    	table { margin:0 !important; }
    	.ibox-content { padding:15px; }
    	.empty { height:400px; background:url("${basepath}/vendor/img/list_empty.png") no-repeat center; }
    </style>
<%};%>
<%var jstpl = {%>
<script src="${basepath}/weixin/plugin/clipboard.js"></script>
<script type="text/javascript">
$(function(){
	clip = new Clipboard(".link");
	clip.on("success",function(e){ layer.msg("复制成功！"); });
	clip.on("error",function(e){ layer.msg("复制失败，请重试！"); });

$("body").on("click","[data-dismiss='popover_link']",function(){
  $("[data-toggle='popover_link']").popover("hide");
})

  
    
    var cp_html=  $("#tpl_cpaddr").html();
    $("[data-toggle='popover_link']").popover({
        html:true,
        title:'',
        content:function(){
            var data = {id:$(this).data("tagid")}
        	 var text_html=null;
             laytpl(cp_html).render(data,function(now_html){
            	text_html=now_html;
             })
           return text_html;
        }
     });
    
    
  })

</script>
<%};%>
<%layout("../layout/_layout.html",{title:'小金娱',jstpl:jstpl,csstpl:csstpl}){ %>
<div style="display: none" id="tpl_cpaddr">
<form class="form-inline">
	<div class="form-group">
	        <div class="input-append">
	            <input type="text" class="form-control"  readonly="" value="http://wx.shandian001.com/wx/product/type/{{d.id}}?shopid=${shopid!}">
	            <a type="button"  style="padding-left: 0px" class="link"  data-dismiss='popover_link'  data-clipboard-text="http://wx.shandian001.com/wx/product/type/{{d.id}}?shopid=${shopid!}">复制</a>
	        </div>
	</div>
		</form>
</div>
    <div class="wrapper wrapper-content animated fadeInUp">
        <div class="row">
            <div class="col-sm-12">

                <div class="ibox">
                    <div class="ibox-title">
                        <h5>所有商品分类</h5>
                        <div class="ibox-tools">
                            <a href="javascript:location.reload();" class="btn btn-warning btn-xs btn_reload"><i class="fa fa-refresh"></i> 刷新</a>
                            <a onclick="add();" class="btn btn-primary btn-xs">创建新分类</a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="row m-b-sm m-t-sm">
                            <div class="col-md-12">
                                <div class="input-group">
                                    <input type="text" placeholder="请输入类型名称" class="input-sm form-control"> <span class="input-group-btn">
                                        <button type="button" class="btn btn-sm btn-primary"> 搜索</button> </span>
                                </div>
                            </div>
                        </div>

                        <div class="project-list">

                            <table class="table table-hover" style="margin-top:25px;">
							<tbody id="type">
								<thead>
                                     <tr>
                                     <th>编号(id)</th>
                                     <th>状态  </th>
                                     <th>类型名称 </th>
                                      <th>排序 </th>
                                    <!--  <th>产品数目 </th> -->
                                    <th>创建时间 </th>
                                    <th>产品数目 </th>
                                      <th>操作 </th>
                                     </tr>
                                </thead>
                                  <% for(type in type_list){  %>
                                  <tr>
                                    <td>${type.id!}</td>
                                   
                                    <%  if(type.state==0){  %>
									<td class="project-status"><span class="label label-primary">正常 </td>
									<%  } else {  %>
									<td class="project-status"><span class="label label-warning">禁用 </td>
									<% }  %>
									<td class="project-title t${type.id!}">${type.type_name!}</td>
									<td class="project-title s${type.id!}">${type.sort!}</td>
									<td><small>${type.createtime!,"yyyy-MM-dd HH:mm:ss"}</small></td>
								    <td class="project-title ">${type.product_num!0}</td>
									<!-- <td class="project-actions"><a  id="search1"   href="projects.html#"
										class="btn btn-white btn-sm"><i class="fa fa-folder"></i>
											保存 </a> <a href="#"  id="edit1"  class="btn btn-white btn-sm edit"><i
											class="fa fa-pencil"></i> 编辑 </a></td>
								     </tr> -->
								      <td class="project-actions">
								       <%  if(type.state==1){  %>
								      <a  id="start${type.id!}" data="${type.id!}"  class="btn btn-white btn-sm start"><i class="fa fa-pencil"></i> 开启 </a>
								      <%  }  %>
								      <a href="#"  id="edit${type.id!}" data="${type.id!}"  class="btn btn-white btn-sm edit"><i class="fa fa-pencil"></i> 编辑 </a>
								      <a href="javascript:gx.del('/shangjia/productType/delete/${type.id!}')" class="btn btn-white btn-sm"><i class="fa fa-trash"></i> 禁用 </a>
								      <a style="display:none" id="save${type.id!}" data="${type.id!}" onclick="editsave(${type.id!})" class="btn btn-white btn-sm editsave${type.id!}"><i class="fa fa-folder"></i> 保存 </a>
								      <a href="javascript:;" data-tagid="${type.id!}"  data-placement="left"  data-toggle="popover_link"  id="edit1"  class="btn btn-white btn-sm "><i class="fa fa-link"> </i> 连接地址 </a>
										   
								      </td>
								     </tr>
                                  <%  }  %>
							</tbody>
                                </table>
                                <% if(isEmpty(type_list)){ %>
                            	<div class="empty"></div>
                            	<% } %>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<%}%>
<script>
$(function(){
	//alert(88);
	$("#loading-example-btn").click(function(){
		window.location.reload();
	});
})

$(".edit").click(function(){
	  var id=$(this).attr("data");
	  //alert(id);
	  $('#edit'+id).hide();
	  $('#save'+id).show();
	  //alert($(".t"+id).text());
	  var t_this = $(this);
	  //console.info(t_this);
	  var list=$(t_this).parent().parent().find("td:eq(2)");
	  var list_sort=$(t_this).parent().parent().find("td:eq(3)");
	  //console.info(list);
	  $.each(list,function(i,obj){
	  $(obj).html("<input type='text' id='name"+id+"' class='input-sm' value='"+$(obj).text().trim()+"'/>");
	  }); 
	  $.each(list_sort,function(i,obj){
		  $(obj).html("<input type='text' id='sort"+id+"' class='input-sm' value='"+$(obj).text().trim()+"'/>");
		  }); 
      //$(this).hide();
      //$('#edit1').show(); 
	})
	
	 function editsave(id){
			//alert(id);
			var type_name=$("#name"+id).val();
			var type_sort=$("#sort"+id).val();
			var ajaxCallUrl="/shangjia/productType/edit_save";
			$.ajax({
		        cache: true,
		        type: "POST",
		        url:ajaxCallUrl,
		        data:{"id":id,"name":type_name,"sort":type_sort},// 你的formid
		        async: false,
		        error: function(request) {
		            layer.msg("Connection error");
		        },
		        success: function(data) {
		            //$("#commonLayout_appcreshi").parent().html(data);
		            //alert(data);
		            if(data.state==true){
		          	  //layer.closeAll();
		          	  layer.msg("修改成功!");
		          	  //window.location.href="/userCenter";
		          	  window.location.href="/shangjia/productType"; 
		            }
		            else{
		          	  layer.closeAll();
		          	  layer.msg(data.msg);
		            }
		        }
		    });
		}
	
	
function add(){
	  var newtr=document.createElement("tr");
	  var newtd_id=document.createElement("td");
	  var newtd_title=document.createElement("td")
	  var newtd_sort=document.createElement("td")
	  var newtd_state=document.createElement("td")
	  var newtd_time=document.createElement("td")
	  var newtd_operate=document.createElement("td")
	  newtd_id.id="id"
	  newtd_title.id="title"
	  newtd_sort.id="sort"
	  newtd_state.id="state"
	  newtd_time.id="time"
	  newtd_operate.id="operate"
	  /* newtd_id.innerHTML="hello world" */
	  newtr.appendChild(newtd_id)
	  newtr.appendChild(newtd_state)
	   newtr.appendChild(newtd_title)
	  newtr.appendChild(newtd_time)
	  newtr.appendChild(newtd_operate)
	   newtr.appendChild(newtd_sort)
	  document.getElementById('type').appendChild(newtr);
	  //document.getElementById("id").innerHTML='<input type="text" id="id" value="'+"myText"+'" />';
	  document.getElementById("title").innerHTML='<input type="text" class="input-sm" placeholder="'+"请输入类型名称"+'" id="type_name" value="'+""+'" />';
	  document.getElementById("sort").innerHTML='<input type="text" class="input-sm" placeholder="'+"请输入排序"+'" id="type_sort" value="'+""+'" />';
	  //document.getElementById("state").innerHTML='<input type="text" id="state" value="'+"myText"+'" />';
	 // document.getElementById("time").innerHTML='<input type="text" id="time" value="'+"myText"+'" />';
	  var btn="<a  id='addsave'    class='btn btn-white btn-sm'><i class='fa fa-folder'></i>保存 </a>";
	  document.getElementById("operate").innerHTML=btn;
	  
	  /* 
		alert(type_name); */
	  //绑定事件
	  $("#addsave").click(function(){
			//alert(888);
			var type_name=$("#type_name").val();
			var type_sort=$("#type_sort").val();
			var ajaxCallUrl="/shangjia/productType/add_save";
			$.ajax({
		        cache: true,
		        type: "POST",
		        url:ajaxCallUrl,
		        data:{"title":type_name,"sort":type_sort},// 你的formid
		        async: false,
		        error: function(request) {
		            layer.msg("Connection error");
		        },
		        success: function(data) {
		            //$("#commonLayout_appcreshi").parent().html(data);
		            //alert(data);
		            if(data.state==true){
		          	  //layer.closeAll();
		          	  layer.msg("增加成功!");
		          	  //window.location.href="/userCenter";
		          	  location.replace("/shangjia/productType"); 
		            }
		            else{
		          	  layer.closeAll();
		          	  layer.msg(data.msg);
		            }
		        }
		    });
		})

      
}

//fang  2016-06-27
//开启   
$(".start").click(function(){
	 var type_id=$(this).attr("data");
		$.ajax({
	        cache: true,
	        type: "POST",
	        url:"/shangjia/productType/start",
	        data:{"id":type_id},// 你的formid
	        async: false,
	        error: function(request) {
	            layer.msg("Connection error");
	        },
	        success: function(data) {
	            //$("#commonLayout_appcreshi").parent().html(data);
	            //alert(data);
	            if(data.state==true){
	          	  //layer.closeAll();
	          	  layer.msg("开启成功!");
	          	  //window.location.href="/userCenter";
	          	  window.location.reload();
	            }
	            else{
	          	  layer.closeAll();
	          	  layer.msg(data.msg);
	            }
	        }
	    });
})


</script>